package exam.test4;

import java.util.ArrayList;

class BinaryMinHeap {
    ArrayList<Integer> items;

    public BinaryMinHeap() {
        items = new ArrayList<Integer>();
    }

    private void swap(int i, int j) {
        int iVal = items.get(i);
        int jVal = items.get(j);
        items.set(j, iVal);
        items.set(i, jVal);
    }

    public void enqueue(int newVal) {
        items.add(newVal);
        int index = items.size();
        while (index > 1 && items.get(index - 1) < items.get(index / 2 - 1)) {
            swap(index / 2 - 1, index - 1);
            index = index / 2;
        }
    }

    public static void main(String[] args) {
        BinaryMinHeap heap = new BinaryMinHeap();
        heap.enqueue(1);
        heap.enqueue(8);
        heap.enqueue(3);
        heap.enqueue(9);
        heap.enqueue(4);
        heap.enqueue(0);
        System.out.println(heap.items);

    }
}